package com.dhcc.bpm.modules.cost.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.modules.cost.entity.CostRecord;
import com.dhcc.bpm.modules.cost.service.ICostRecordService;
import com.dhcc.bpm.modules.cost.vo.ApiCostVO;
import com.dhcc.bpm.modules.cost.vo.AppCostVO;
import com.dhcc.bpm.modules.cost.vo.CostRecordVO;
import com.dhcc.bpm.modules.cost.vo.CostGatewayLogVO;
import com.dhcc.bpm.modules.log.service.IGatewayLogDayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


/**
 * @ClassName CostRecordController.class
 * @Description  消费记录表 控制器
 * @Author xiaFaYu
 * @Date 2020-02-03 14:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("bpm-cost/costRecord")
@Api(value = "消费记录表", tags = "消费记录表接口")
public class CostRecordController extends BladeController {

	private ICostRecordService costRecordService;
	private IGatewayLogDayService gatewayLogDayService;

	/**
	 * @Description 详情
	 * @Author xiafayu
	 * @Date 2020/2/24 14:59
	 * @param costRecord
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.cost.entity.CostRecord>
	 **/
	@ApiLog("消费记录表详情")
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入costRecord")
	public R<CostRecord> detail(CostRecord costRecord) {
		CostRecord detail = costRecordService.getOne(Condition.getQueryWrapper(costRecord));
		return R.data(detail);
	}

	/**
	 * @Description 分页 消费记录表
	 * @Author xiafayu
	 * @Date 2020/2/24 14:59
	 * @param costRecord
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.cost.entity.CostRecord>>
	 **/
	@ApiLog("消费记录表分页")
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入costRecord")
	@Log(title = "查询消费记录", businessType = BusinessType.SELECT)
	public R<IPage<CostRecord>> list(CostRecord costRecord, Query query) {
		IPage<CostRecord> pages = costRecordService.page(Condition.getPage(query), Condition.getQueryWrapper(costRecord));
		return R.data(pages);
	}

	/**
	 * @Description 自定义分页 消费记录表
	 * @Author xiafayu
	 * @Date 2020/2/24 14:59
	 * @param costRecord
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.cost.vo.CostRecordVO>>
	 **/
	@ApiLog("消费记录表分页")
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入costRecord")
	@Log(title = "查询消费记录", businessType = BusinessType.SELECT)
	public R<IPage<CostRecordVO>> page(CostRecordVO costRecord, Query query) {
		IPage<CostRecordVO> pages = costRecordService.selectCostRecordPage(Condition.getPage(query), costRecord);
		return R.data(pages);
	}

	/**
	 * @Description 新增 消费记录表
	 * @Author xiafayu
	 * @Date 2020/2/24 14:59
	 * @param costRecord
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("消费记录表新增")
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入costRecord")
	public R save(@Valid @RequestBody CostRecord costRecord) {
		return R.status(costRecordService.save(costRecord));
	}

	/**
	 * @Description 修改 消费记录表
	 * @Author xiafayu
	 * @Date 2020/2/24 14:59
	 * @param costRecord
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("消费记录表修改")
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入costRecord")
	public R update(@Valid @RequestBody CostRecord costRecord) {
		return R.status(costRecordService.updateById(costRecord));
	}

	/**
	 * @Description 新增或修改 消费记录表
	 * @Author xiafayu
	 * @Date 2020/2/24 14:59
	 * @param costRecord
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("消费记录表新增修改")
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入costRecord")
	public R submit(@Valid @RequestBody CostRecord costRecord) {
		return R.status(costRecordService.saveOrUpdate(costRecord));
	}

	
	/**
	 * @Description 删除 消费记录表
	 * @Author xiafayu
	 * @Date 2020/2/24 15:00
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("消费记录表删除")
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(costRecordService.removeByIds(Func.toLongList(ids)));
	}

	/**
	 * @Description api收费统计
	 * @Author xiafayu
	 * @Date 2020/2/24 15:00
	 * @param apiCostVO
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.cost.vo.ApiCostVO>>
	 **/
	@ApiLog("查询API收费")
	@GetMapping("/costApiIncrease")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "收费api", notes = "传入apiCostVO")
	@Log(title = "查询API收费", businessType = BusinessType.SELECT)
	public R<IPage<ApiCostVO>> costApiIncrease(ApiCostVO apiCostVO, Query query) {
		IPage<ApiCostVO> pages = costRecordService.selectApiCostRecordPage(Condition.getPage(query), apiCostVO);
		List<ApiCostVO>  apiList = pages.getRecords();
		List<ApiCostVO> list = new ArrayList<>();
		for(ApiCostVO apiCost : apiList){
			CostGatewayLogVO gatewayLog = costRecordService.getApiCostByApiCode(apiCost.getApiCode(), apiCostVO.getStartDate(), apiCostVO.getEndDate());
			if(gatewayLog != null){
				apiCost.setRate(gatewayLog.getRate());
				apiCost.setSuccessNum(gatewayLog.getSuccessNum());
				apiCost.setNum(gatewayLog.getNum());
				apiCost.setUseTime(gatewayLog.getUseTime());
			}
			list.add(apiCost);
		}
		pages.setRecords(list);
		return R.data(pages);
	}

	/**
	 * @Description 查询渠道收费
	 * @Author xiafayu
	 * @Date 2020/2/24 15:00
	 * @param appCostVO
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.cost.vo.AppCostVO>>
	 **/
	@ApiLog("查询渠道收费")
	@GetMapping("/costAppIncrease")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "收费app", notes = "传入appCostVO")
	@Log(title = "查询渠道收费", businessType = BusinessType.SELECT)
	public R<IPage<AppCostVO>> costAppIncrease(AppCostVO appCostVO, Query query) {
		IPage<AppCostVO> pages = costRecordService.selectAppCostRecordPage(Condition.getPage(query), appCostVO);
		List<AppCostVO>  appList = pages.getRecords();
		List<AppCostVO> list = new ArrayList<>();
		for(AppCostVO appCost : appList){
			CostGatewayLogVO gatewayLog = costRecordService.getApiCostByApiCode(appCost.getAppCode(), appCost.getStartDate(), appCost.getEndDate());
			if(gatewayLog != null){
				appCost.setRate(gatewayLog.getRate());
				appCost.setSuccessNum(gatewayLog.getSuccessNum());
				appCost.setNum(gatewayLog.getNum());
				appCost.setUseTime(gatewayLog.getUseTime());
			}

			list.add(appCost);
		}
		pages.setRecords(list);
		return R.data(pages);
	}
}
